package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureSelectAdapter;
import com.bhs.sansong.adapter.TypeAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.PictureBean;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 包装
 */
public class PackagingDialog extends Dialog implements View.OnClickListener {

    private RecyclerView mRvList;
    private RelativeLayout mRlOther;
    private EditText mEtOther;
    private TextView mTvNumber;
    private RecyclerView mRvPicture;
    private RadioButton mRbYes;
    private RadioButton mRbNo;
    private LinearLayout mLlSize;
    private boolean isHaveChosen;//是否选了包装类型
    private boolean isPerfect;//包装类型是否完善
    private TypeAdapter typeAdapter;
    private List<String> data;
    private String packType;
    public PictureSelectAdapter selectAdapter;
    int maxSelectNum = 3;
    private BaseActivity baseActivity;
    private EditText mEtLength;
    private EditText mEtWidth;
    private EditText mEtTall;
    public String sLength;
    public String sWidth;
    public String sTall;
    public String images;
    public boolean isSpecial;

    public PackagingDialog(@NonNull BaseActivity context, List<String> data) {
        super(context, R.style.MyBottomDialogStyle);
        this.data = data;
        this.baseActivity = context;
    }

    public PackagingDialog(@NonNull BaseActivity context) {
        super(context, R.style.MyBottomDialogStyle);
        this.baseActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_packaging);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_close).setOnClickListener(this);
        mRvList = findViewById(R.id.rv_list);
        mRlOther = findViewById(R.id.rl_other);
        mEtOther = findViewById(R.id.et_other);
        mTvNumber = findViewById(R.id.tv_number);
        mRvPicture = findViewById(R.id.rv_picture);
        mRbYes = findViewById(R.id.rb_yes);
        mRbNo = findViewById(R.id.rb_no);
        mLlSize = findViewById(R.id.ll_size);
        mEtLength = findViewById(R.id.et_length);
        mEtWidth = findViewById(R.id.et_width);
        mEtTall = findViewById(R.id.et_tall);
        List<PictureBean> companyList = new ArrayList<>();
        selectAdapter = new PictureSelectAdapter(maxSelectNum, baseActivity);
        if (!TextUtils.isEmpty(images)) {
            String[] picture = images.split("\\|\\|");
            for (String s : picture) {
                selectAdapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(s));
                companyList.add(new PictureBean(s, s));
            }
        }
        mRvPicture.setAdapter(selectAdapter);
        mRvPicture.setLayoutManager(new GridLayoutManager(getContext(), 3));
        if (maxSelectNum > companyList.size()) {
            companyList.add(new PictureBean(2));
        }
        selectAdapter.setNewData(companyList);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        List<TabBean> mList = new ArrayList<>();
        for (String datum : data) {
            if (!TextUtils.isEmpty(packType) && datum.equals(packType)) {
                mList.add(new TabBean(datum, true));
                isPerfect = true;
                isHaveChosen = true;
            } else {
                mList.add(new TabBean(datum));
            }
        }
        if (!TextUtils.isEmpty(packType) && !isPerfect) {
            mList.get(mList.size() - 1).setChecked(true);
            mEtOther.setText(packType);
            mRlOther.setVisibility(View.VISIBLE);
            isPerfect = true;
            isHaveChosen = true;
        }
        if (isSpecial) {
            mRbYes.setChecked(true);
            mEtLength.setText(sLength);
            mEtWidth.setText(sWidth);
            mEtTall.setText(sTall);
            mLlSize.setVisibility(View.VISIBLE);
        } else {
            mRbNo.setChecked(true);
        }
        typeAdapter = new TypeAdapter(mList);
        mRvList.setAdapter(typeAdapter);
        mRvList.setLayoutManager(new GridLayoutManager(getContext(), 4));
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                isHaveChosen = true;
                for (TabBean datum : typeAdapter.getData()) {
                    datum.setChecked(false);
                }
                packType = typeAdapter.getData().get(position).getTypeName();
                if (packType.equals("其他")) {
                    mRlOther.setVisibility(View.VISIBLE);
                } else {
                    mRlOther.setVisibility(View.GONE);
                }
                typeAdapter.getData().get(position).setChecked(true);
                typeAdapter.notifyDataSetChanged();
            }
        });
        mRbYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mLlSize.setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });

    }

    @Override
    public void onClick(View view) {
        dismiss();
        if (view.getId() == R.id.btn_confirm) {
            if (!isHaveChosen) {
                ToastUtil.show("请选择包装类型");
                return;
            }
            if (packType.equals("其他")) {
                if (TextUtils.isEmpty(mEtOther.getText())) {
                    ToastUtil.show("请输入包装类型");
                    return;
                }
            }
            if (mRbYes.isChecked()) {
                if (TextUtils.isEmpty(mEtLength.getText()) ||
                        TextUtils.isEmpty(mEtWidth.getText()) || TextUtils.isEmpty(mEtTall.getText())) {
                    ToastUtil.show("请完善异性件尺寸");
                    return;
                }
            }
            isPerfect = true;
            listener.onClick("", getPackType());
        }
    }

    public String getPackType() {
        if (packType.equals("其他")) {
            return mEtOther.getText().toString();
        } else {
            return packType;
        }
    }


    public void setPackType(String packType) {
        this.packType = packType;
    }

    public String isSpecial() {
        if (mRbYes != null) {
            return mRbYes.isChecked() ? "1" : "0";
        } else {
            return isSpecial ? "1" : "0";
        }
    }

    public String sLength() {
        if (mEtLength != null) {
            return mEtLength.getText().toString();
        } else {
            return sLength;
        }
    }

    public String sWidth() {
        if (mEtWidth != null) {
            return mEtWidth.getText().toString();
        } else {
            return sWidth;
        }
    }

    public String sTall() {
        if (mEtTall != null) {
            return mEtTall.getText().toString();
        } else {
            return sTall;
        }
    }

    public boolean isPerfect() {
        return isPerfect;
    }

    public void setPerfect(boolean perfect) {
        isPerfect = perfect;
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    public List<String> getData() {
        return data;
    }

    public void setData(List<String> data) {
        this.data = data;
    }

    public String getImages() {
        if (selectAdapter != null) {
            return selectAdapter.getImages();
        } else {
            return images;
        }
    }
}
